package com.learn.use.myreconsituteproject.register.model;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.learn.use.myreconsituteproject.http.Urls;
import com.learn.use.myreconsituteproject.mvp.codebean.CodeBean;
import com.learn.use.myreconsituteproject.register.bean.RegisterNextBean;
import com.learn.use.myreconsituteproject.register.presenter.RegisterPresenter;
import com.learn.use.myreconsituteproject.util.NetworkUtils;
import com.learn.use.myreconsituteproject.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by 聪明一只哈 on 2018/9/12.
 * Time: 2018/9/12  11:39
 */
public class RegisterModel {


    /**
     * 获得验证码
     *
     * @param ase
     * @param num
     * @param registerPresenter
     */
    public void getRegisterData(String ase, String num, final RegisterPresenter registerPresenter) {
        OkGo.<String>post(Urls.IDEN_CODE)
                .tag(this)
                .params("AES", ase)
                .params("phone", num)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String data = response.body();
                        if (data != null) {
                            CodeBean codeBean = JSON.parseObject(data, CodeBean.class);
                            registerPresenter.codeSuccess(codeBean, 0);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        registerPresenter.codeError(response.message(), 1);

                    }
                });
    }

    /**
     * 注册账户
     *
     * @param num
     * @param pass
     * @param code
     * @param aes
     * @param registerPresenter
     */
    public void RegisterDataCount(Context context, String num, String pass, String code, String aes, final RegisterPresenter registerPresenter) {

        if (NetworkUtils.isConnected(context)) {
            OkGo.<String>post(Urls.REGISTER)
                    .params("phone", num)
                    .params("password", pass)
                    .params("code", code)
                    .params("AES", aes)

                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            String body = response.body();
                            if (body != null && !TextUtils.isEmpty(body)) {
                                RegisterNextBean registerNextBean = JSON.parseObject(body, RegisterNextBean.class);
                                registerPresenter.registedSuccess(registerNextBean);
                            }


                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            registerPresenter.registeredFiled(response.message());

                        }
                    });

        } else {
            ToastUtils.showShortToast(context, "网络链接失败，请检查网络设置");
        }


        /*

        Map<String,String> params=new HashMap<>();
        params.put("phone",num);
        params.put("password",pass);
        params.put("code",code);
        params.put("code",code);

        OkGoUtils.postRequest(context,Urls.REGISTER,params,new Request(){

            @Override
            public void onSuccess(String json) {
                super.onSuccess(json);
            }

            @Override
            public void onError(String error) {
                super.onError(error);

            }
        });*/
    }


}
